import 'package:flutter/material.dart';
import 'package:github/server.dart';
import 'package:ok_github/page/route/route.dart';
import 'package:ok_github/utils/route_helper.dart';
import 'package:ok_image/ok_image.dart';

import '../../repo/user_repo.dart';
import '../../widget/widget.dart';

class GithubDrawer extends StatefulWidget {
  @override
  _GithubDrawerState createState() => _GithubDrawerState();
}

class _GithubDrawerState extends State<GithubDrawer> {
  var _userRepo = UserRepo();

  @override
  void initState() {
    super.initState();
    _userRepo.refreshUserInfo();
  }

  @override
  Widget build(BuildContext context) {
    return Drawer(
      child: buildStreamBuilder(),
    );
  }

  StreamBuilder<CurrentUser> buildStreamBuilder() {
    return StreamBuilder<CurrentUser>(
        stream: _userRepo.currentUserStream,
        initialData: _userRepo.currentUser,
        builder: (context, snapshot) {
          var user = snapshot.data;
          var column = Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              buildHeader(user),
              GlobalWidget.space,
              _buildItem(
                Icons.notifications,
                "我的消息",
                nextPage: routes.notification,
              ),
              _buildItem(Icons.exit_to_app, "登出", onTap: _logout),
            ],
          );
          return SafeArea(
            top: false,
            child: SingleChildScrollView(
              child: column,
            ),
          );
        });
  }

  Color get titleColor {
    var theme = Theme.of(context);
    return theme.primaryTextTheme.title.color;
  }

  Widget buildHeader(CurrentUser user) {
    var textStyle = TextStyle(color: titleColor);
    var themeData = Theme.of(context);
    return Container(
      padding: const EdgeInsets.symmetric(vertical: 16.0),
      color: themeData.primaryColor,
      child: Column(
        children: <Widget>[
          Container(
            height: MediaQuery.of(context).padding.top,
          ),
          Align(
            alignment: Alignment.center,
            child: ClipRRect(
              borderRadius: BorderRadius.circular(50.0),
              child: Container(
                width: 100.0,
                height: 100.0,
                child: OKImage(
                  url: user.avatarUrl,
                  width: 80.0,
                  height: 80.0,
                ),
              ),
            ),
          ),
          GlobalWidget.space,
          DefaultTextStyle(
            style: textStyle,
            child: Text(
              user.login,
              style: textStyle,
            ),
          ),
          GlobalWidget.space,
          Text(
            user.email,
            style: textStyle,
          ),
        ],
      ),
    );
  }

  Widget _buildItem(
    IconData iconData,
    String title, {
    String nextPage,
    Function onTap,
  }) {
    return Column(
      children: <Widget>[
        GlobalWidget.space,
        ListTile(
          leading: Icon(iconData),
          title: Text(title),
          onTap: onTap ??
              () => RouteHelper.pushNamed(
                    context,
                    nextPage,
                  ),
        ),
      ],
    );
  }

  _logout() {
    RouteHelper.pushNamed(context, routes.login, replaceRoot: true);
  }
}
